﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace XxSystem.FileManager.core
{
    public class CusResultFilter : IResultFilter
    {
        public void OnResultExecuted(ResultExecutedContext context)
        {

        }

        public void OnResultExecuting(ResultExecutingContext context)
        {
            if (context.Result is OkResult)
            {
                var resp = new BaseResult
                {
                    Status = context.HttpContext.Response.StatusCode,
                    Id = Guid.NewGuid().ToString(),
                };
                context.Result = new OkObjectResult(resp);

            }
            else if (context.Result is ObjectResult objectResult)
            {
                var resp = new BaseResult
                {
                    Result = objectResult.Value,
                    Id = Guid.NewGuid().ToString(),
                    Status = context.HttpContext.Response.StatusCode
                };
                objectResult.Value = resp;

            }
            else if (context.Result is JsonResult jsonResult)
            {
                var resp = new BaseResult
                {
                    Result = jsonResult.Value,
                    Status = context.HttpContext.Response.StatusCode,
                    Id = Guid.NewGuid().ToString()
                };
                jsonResult.Value = resp;
            }
        }
    }
}
